﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生选课及成绩查询管理系统
{
    public partial class updateCourseInfo : Form
    {
        public updateCourseInfo()
        {
            InitializeComponent();
        }

        private void txt_cname_Click(object sender, EventArgs e)
        {
            string cno = txt_cno.Text;
            string sql = "select count(*) from dbo.course where cno=@cno";
            SqlParameter[] arraysql = { new SqlParameter("@cno", cno) };
            object o = DBAccess.GetOne(sql, arraysql);
            if ((int)o == 1)
            {
                txt_tishi.Text = "✓";
                txt_tishi.ForeColor = Color.Green;
            }
            else
            {
                MessageBox.Show("请输入正确的课程号");
                txt_cno.Focus();
                return;
            }
        }

        private void btn_sub_Click(object sender, EventArgs e)
        {
            string cno = txt_cno.Text;
            string cname = txt_cname.Text;
            string tno = txt_tno.Text;
            string credit = txt_credit.Text;
            string cproperties = txt_cproperties.Text;
            string cCount = txt_cCount.Text;
            if (string.IsNullOrEmpty(cno) || string.IsNullOrEmpty(cname) || string.IsNullOrEmpty(tno) || string.IsNullOrEmpty(credit) || string.IsNullOrEmpty(cproperties))
            {
                MessageBox.Show("信息输入不完整");
                return;
            }
            string sql = "update dbo.course set cname=@cname,tno=@tno,credit=@credit,cCount=@cCount,cproperties=@cproperties where cno=@cno";
            SqlParameter[] arraysql = { new SqlParameter("@cname", cname), new SqlParameter("@tno", tno),
                                      new SqlParameter("@credit",credit),new SqlParameter("@cno",cno),
                                      new SqlParameter("@cCount",cCount),new SqlParameter("@cproperties",cproperties)}; 
            int res = DBAccess.GetState(sql, arraysql);
            if (res != 0)
            {
                MessageBox.Show("修改该课程信息成功");
                this.Dispose();
            }
            else
            {
                MessageBox.Show("修改失败");
            }


        }

        private void txt_credit_Click(object sender, EventArgs e)
        {
            string tno = txt_tno.Text;
            if(tno==""){
                return;
            }
            string sql = "select count(*) from teacher where tno=" + tno;
            object res = DBAccess.GetOne(sql);
            if ((int)res == 0)
            {
                MessageBox.Show("没有该老师！请重新填写");
                txt_tno.Focus();
                return;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
